package com.kuzlak.cms.security;


import com.kuzlak.cms.module.sys.entity.*;
import com.kuzlak.cms.module.sys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 从数据库中获取用户信息,将用户信息送到SpringSecurity上下文中
 */
@Transactional
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysAccountsService accountsService;
    @Autowired
    private SysUsersService usersService;
    @Autowired
    private SysUserRoleService userRoleMapper;
    @Autowired
    private SysRoleService roleMapper;
    @Autowired
    private SysRolePermissionService rolePermissionMapper;
    @Autowired
    private SysPermissionService permissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysAccounts user = accountsService.getUserByPhone(username);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException(username);
        }
        SysUsers users = usersService.getById(user.getUserId());
        //当前用户角色名称列表
        List<String> roleNameList = new ArrayList<>();
        //当前用户的权限名称列表
        List<String> permissionNameList = new ArrayList<>();
        //获取用户角色列表
        List<SysUserRole> userRoles = userRoleMapper.getUserRoleByUserId(user.getUserId());
        if (!CollectionUtils.isEmpty(userRoles)) {
            List<Long> roleIdList = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(roleIdList)) {
                List<SysRole> roles = roleMapper.batchGetRolesByRoleIds(roleIdList);
                if (!CollectionUtils.isEmpty(roles)) {
                    List<String> roleNames = roles.stream().map(SysRole::getCode).collect(Collectors.toList());
                    roleNameList.addAll(roleNames);
                }

                //当前角色下的权限列表
                List<SysRolePermission> rolePermissions = rolePermissionMapper.batchGetRolePermissionsByRoleIds(roleIdList);
                if (!CollectionUtils.isEmpty(rolePermissions)) {
                    List<Long> permissionIdList = rolePermissions.stream().map(SysRolePermission::getPermissionId).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(permissionIdList)) {
                        List<SysPermission> permissions = permissionMapper.batchGetPermissionsByPermissionIds(permissionIdList);
                        if (!CollectionUtils.isEmpty(permissions)) {
                            List<String> permissionNames = permissions.stream().map(SysPermission::getCode).collect(Collectors.toList());
                            permissionNameList.addAll(permissionNames);
                        }
                    }
                }
            }
        }

        return new LoginUserDetails(user,users, roleNameList, permissionNameList);
    }
}
